package com.dao;

import com.entity.Message;
import com.util.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by wlc on 2018/5/6.
 */
public class MessageDao {
    /**
     * 将消息文件保存到接收用户目录下
     * @param messagePath
     * @param message
     * @throws IOException
     */
    public static void saveMessage(String messagePath, Message message) throws IOException{
        FileUtil.createFile(messagePath);
        FileUtil.writeFile(messagePath,message.messageToList());
    }

    /**
     * 获取指定目录下的消息文件数
     * @param messageDir
     * @return
     */
    public static String getMessageFileNum(String messageDir){
        ArrayList<File> messageFiles = FileUtil.readFileCatalog(messageDir);

        return String.valueOf(messageFiles.size());
    }

    /**
     * 读取出指定目录下的所有消息文件
     * @param messageDir
     * @return
     * @throws IOException
     */
    public static ArrayList<Message> getAllMessageContent(String messageDir) throws IOException{
        ArrayList<File> messageFiles = FileUtil.readFileCatalog(messageDir);
        ArrayList<Message> messages = new ArrayList<>();
        if (messageFiles.size()>=1){
            for (File file :messageFiles){
                Message message = new Message();
                message.listToMessage(FileUtil.readFile(file.getAbsolutePath()));
                messages.add(message);
            }
        }
        return messages;
    }

    public static Message getMessageFile(String messagePath) throws IOException{
        Message message = new Message();
        message.listToMessage(FileUtil.readFile(messagePath));
        return message;
    }

    /**
     * 删除指定路径下的消息文件
     * @param messagePath
     */
    public static void delMessageFile(String messagePath){
        FileUtil.deleteDir(new File(messagePath));
    }

    /**
     * 标记指定路径下的消息文件为已读
     * @param messagePath
     * @throws IOException
     */
    public static void markMessageFileRead(String messagePath) throws IOException{
        ArrayList<String> messageContent = FileUtil.readFile(messagePath);
        messageContent.set(9,"1");
        FileUtil.writeFile(messagePath,messageContent);
    }

    /**
     * 读取指定目录下图片文件
     * @param destPath
     */
    public static ArrayList<File> getImageFile(String destPath) {
       return FileUtil.readImageFile(destPath);
    }
}
